home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_006 / microemacs / termio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  163 lines

  1. /*
  2.  * The functions in this file negotiate with the operating system for
  3.  * characters, and write characters in a barely buffered fashion on the display.
  4.  * All operating systems.
  5.  */
  6. #include <exec/types.h>
  7. #include <exec/exec.h>
  8. #include <intuition/intuition.h>
  9. #include <devices/console.h>
  10. #include <stdio.h>
  11. #include "keymap.h"
  12.  
  13. struct IntuitionBase *IntuitionBase;
  14. #define INTUITION_REV 29
  15.  
  16. static struct NewWindow NewWindow = {
  17.    0, 0,
  18.    640, 200,
  19.    -1, -1,
  20.    0,
  21.    SMART_REFRESH | ACTIVATE | WINDOWDRAG | WINDOWDEPTH | WINDOWSIZING,
  22.    NULL,
  23.    NULL,
  24.    "MicroEMACS",
  25.    NULL,
  26.    NULL,
  27.    100, 35,
  28.    640, 200,
  29.    WBENCHSCREEN
  30. };
  31.  
  32. static struct Window *Window;
  33. static struct IOStdReq consoleIO;
  34. static struct MsgPort consoleMsgPort;
  35.  
  36. #define NOBUF 1024
  37. static char obuf[NOBUF];
  38. static int nobuf;
  39.  
  40. /*
  41.  * This function fills in the keymap string fields
  42.  */
  43. static void FillIn(keytypes, keymap, n, strings)
  44. UBYTE *keytypes;
  45. UBYTE **keymap;
  46. int n;
  47. char **strings;
  48. {
  49.    int i;
  50.  
  51.    for (i = 0; i < n; ++i)
  52.    {
  53.       if (keytypes[i] & KCF_STRING)
  54.       {
  55.          if (*strings == NULL)
  56.      {  printf("too few KeyStrings\n"); exit(1); }
  57.      keymap[i] = *strings;
  58.      ++strings;
  59.       }
  60.    }
  61.    if (*strings != NULL)
  62.    {  printf("too many KeyStrings\n"); exit(1); }
  63. }
  64.  
  65. /*
  66.  * This function is called once to set up the terminal device streams.
  67.  * On VMS, it translates SYS$INPUT until it finds the terminal, then assigns
  68.  * a channel to it and sets it raw. On CPM it is a no-op.
  69.  */
  70. ttopen()
  71. {
  72.    int i;
  73.  
  74.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",
  75.                             INTUITION_REV);
  76.    if (IntuitionBase == NULL)
  77.    {  printf("can't open Intuition\n"); exit(1); }
  78.    Window = (struct Window *)OpenWindow(&NewWindow);
  79.    if (Window == NULL)
  80.    {  printf("can't open window\n"); exit(1); }
  81.    consoleIO.io_Data = (APTR) Window;
  82.    consoleIO.io_Length = sizeof(*Window);
  83.    if (OpenDevice("console.device", 0, &consoleIO, 0) != 0)
  84.    {  printf("can't open console\n"); exit(1); }
  85.    consoleMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  86.    consoleMsgPort.mp_Flags = 0;
  87.    if ((i =  AllocSignal(-1)) == -1)
  88.    {  printf("can't AllocSignal\n"); exit(1); }
  89.    consoleMsgPort.mp_SigBit = i;
  90.    consoleMsgPort.mp_SigTask = (struct Task *)FindTask(NULL);
  91.    consoleIO.io_Message.mn_ReplyPort = &consoleMsgPort;
  92.  
  93.    FillIn(HiKeyMapTypes, HiKeyMap, 40, HiStrings);
  94.    consoleIO.io_Command = CD_SETKEYMAP;
  95.    consoleIO.io_Data = (APTR) &KeyMap;
  96.    consoleIO.io_Length = sizeof(KeyMap);
  97.    DoIO(&consoleIO);
  98.  
  99.    nobuf = 0;
  100. }
  101.  
  102. /*
  103.  * This function gets called just before we go back home to the command
  104.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  105.  * Another no-operation on CPM.
  106.  */
  107. ttclose()
  108. {
  109.    ttflush();
  110.    CloseDevice(&consoleIO);
  111.    CloseWindow(Window);
  112.    CloseLibrary(IntuitionBase);
  113. }
  114.  
  115. /*
  116.  * Write a character to the display. On VMS, terminal output is buffered, and
  117.  * we just put the characters in the big array, after checking for overflow.
  118.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  119.  * MS-DOS (use the very very raw console output routine).
  120.  */
  121. ttputc(c)
  122. {
  123.         if (nobuf >= NOBUF)
  124.                 ttflush();
  125.         obuf[nobuf++] = c;
  126. }
  127.  
  128. /*
  129.  * Flush terminal buffer. Does real work where the terminal output is buffered
  130.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  131.  */
  132. ttflush()
  133. {
  134.    if (nobuf != 0)
  135.    {
  136.       register int i = nobuf;
  137.       /* The DoIO write to the console trashes D7, so we declare
  138.          an unnecessary register variable here to for it to be saved */
  139.       
  140.       consoleIO.io_Command = CMD_WRITE;
  141.       consoleIO.io_Data = (APTR) obuf;
  142.       consoleIO.io_Length = i;
  143.       DoIO(&consoleIO);
  144.       nobuf = 0;
  145.    }
  146. }
  147.  
  148. /*
  149.  * Read a character from the terminal, performing no editing and doing no echo
  150.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  151.  * simple on CPM, because the system can do exactly what you want.
  152.  */
  153. ttgetc()
  154. {
  155.    char ch;
  156.  
  157.    consoleIO.io_Command = CMD_READ;
  158.    consoleIO.io_Data = (APTR) &ch;
  159.    consoleIO.io_Length = 1;
  160.    DoIO(&consoleIO);
  161.    return((int)ch);
  162. }
  163.